package com.es.spring_es.services;

import com.es.spring_es.clientmap.EsUtils;
import com.es.spring_es.model.Demo;
import com.es.spring_es.model.DriverLocationEntity;
import org.frameworkset.elasticsearch.ElasticSearchException;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xu
 * @date 2020/6/2
 * @desciption 司机位置信息操作类
 */
@Service
public class DriverlocationServices {

    @Autowired
    @Qualifier("bbossESStarterDefault")
    private BBossESStarter bbossESStarterDefault;
  /*  @Autowired
    @Qualifier("bbossESStarterLogs")
    private BBossESStarter bbossESStarterLogs;

    private ClientInterface clientUtil = null;*/


    @Autowired
    EsUtils esUtils;

    /**
     * 创建司机位置信息索引表
     */
    public void testCreateIndice(){
        //创建加载配置文件的客户端工具，单实例多线程安全
        ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient("esmapper/driver_location.xml");


        try {
           // esUtils.CreateDriverLocationMainIndice("driver_location","createDriverLocationIndice");
            //判读索引表demo是否存在，存在返回true，不存在返回false
            boolean exist = clientUtil.existIndice("driver_location");

            //如果索引表demo已经存在先删除mapping
            if(exist) {
               /* String r = clientUtil.dropIndice("driver_location");
                System.out.println(r);
                exist = clientUtil.existIndice("driver_location");
//				r = clientUtil.dropIndice("demo");
//				System.out.println(r);
                String demoIndice = clientUtil.getIndice("driver_location");//获取最新建立的索引表结构
                System.out.println(demoIndice);*/
            }else {
                //创建索引表demo
                clientUtil.createIndiceMapping("driver_location",//索引表名称
                        "createDriverLocationIndice");//索引表mapping dsl脚本名称，在esmapper/demo7.xml中定义createDemoIndice
            }

            String demoIndice = clientUtil.getIndice("driver_location");//获取最新建立的索引表结构
            System.out.println(demoIndice);
        } catch (ElasticSearchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * 存储司机位置信息
     */
    public void addDriverData(List<DriverLocationEntity> list){
        try {
            //ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
            /*String driver_location = clientUtil.addDocuments("driver_location", list,"refresh");
           // System.out.println("多条数据增加返回结果: " +driver_location);

            long l = clientUtil.countAll("driver_location");
            System.out.println("driver_location 表count : "+l);*/

             esUtils.addOrUpdateDocuments("driver_location", list);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询司机位置所有信息
     */
    public void searchDriverLocalAllData(){
        try {
            //ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
           // List<DriverLocationEntity> driver_location = clientUtil.mgetDocuments("driver_location", DriverLocationEntity.class);
/*
            ESDatas<DriverLocationEntity> driver_location1 = clientUtil.searchAll("driver_location", DriverLocationEntity.class);
            List<DriverLocationEntity> datas = driver_location1.getDatas()*/;

            List<DriverLocationEntity> driver_location = esUtils.getDocumentDataAll("driver_location", DriverLocationEntity.class);

            System.out.println(driver_location.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 条件查询
     */
    public List<DriverLocationEntity> searchDriverLocalByTrid(String trid){

        List<DriverLocationEntity> exec = new ArrayList<>();
        try {
         //创建加载配置文件的客户端工具，用来检索文档，单实例多线程安全
            /*ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient("esmapper/driver_location.xml");
            Map<String,Object> params = new HashMap<String,Object>();
            //设置applicationName1和applicationName2两个变量的值
            params.put("trids",222l);

            ESDatas<DriverLocationEntity> esDatas =  //ESDatas包含当前检索的记录集合，最多1000条记录，由dsl中的size属性指定
                    clientUtil.searchList("driver_location/_search",//demo为索引表，_search为检索操作action
                            "searchDriverLocationByTrid",//esmapper/demo.xml中定义的dsl语句
                            params,//变量参数
                            DriverLocationEntity.class);//返回的文档封装对象类型

            List<DriverLocationEntity> datas = esDatas.getDatas();
            System.out.println(datas.toString());*/

            Map<String,Object> params = new HashMap<String,Object>();
            params.put("trid",trid);


            exec = esUtils.exec("driver_location", params, "searchDriverLocationByTrid", DriverLocationEntity.class);

            System.out.println(exec);
            return exec;

        } catch (Exception e) {
            e.printStackTrace();
            return exec;
        }


    }


}
